/*
 *   Copyright (c) 2011 Hitachi Data Systems, Inc.
 *
 *   Permission is hereby granted to  this software and associated
 *   documentation files (the "Software"), subject to the terms and
 *   conditions of the Sample Source Code License (SSCL) delivered
 *   with this Software. If you do not agree to the terms and
 *   conditions of the SSCL,
 *
 *     (i)  you must close this file and delete all copies of the
 *          Software, and
 *     (ii) any permission to use the Software is expressly denied.
 *
 */
package com.hds.hcp.apihelpers.query.request;

/**
* Base class for the query request classes. It contains general utilities to translate
* the query request objects into JSON formatted string representations.
* 
* @version 4,1
*
*/

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

import org.codehaus.jackson.JsonEncoding;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonGenerator.Feature;
import org.codehaus.jackson.map.MappingJsonFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;

abstract class BaseQueryRequest {

	/**
	 * Returns the JSON formatted representation of the object without any "pretty print" readability formatting.
	 */
	public String toString() {
		return toString(false);
	}
	
	/**
	 * Returns the JSON formatted representation of the object and allows the caller to specify
	 * whether "<i>pretty print</i>" formatting should be performed.
	 * <p>
	 * Formatting the content with "pretty print" provides it in a form that makes reading the representation
	 * easier for the human eye.'
	 * <p>
	 * Without "pretty print"
	 * <p>
	 * <pre>{"verbose":true,"count":1000,"systemMetadata":{"changeTime":{"start":0,"end":54885918493}}}</pre>
	 * <p> With "pretty print"
	 * <p>
	 * <pre>
	 * {
	 *     "verbose":true,
	 *     "count":1000,
	 *     "systemMetadata": {
	 *         "changeTime": {
	 *             "start":0,
	 *             "end":54885918493
	 *         }
	 *     }
	 * }
	 * </pre>
	 * @param inPrettyPrint <code>true/false</code> value specifying whether "pretty printing" should be performed.
	 */
	public String toString(boolean inPrettyPrint) {
		OutputStream mOutput = new ByteArrayOutputStream();
		
		try {
			/*
			 * Setup JSON Generator.
			 */
			mOutput = new ByteArrayOutputStream();
			JsonGenerator outGenerator = (new MappingJsonFactory()).createJsonGenerator(mOutput, JsonEncoding.UTF8);

			if (inPrettyPrint) {
				// Make it easy on the eyes.
				outGenerator.useDefaultPrettyPrinter();
			}
			
			// Be a bit lazy and have the generator make sure all JSON items are closed
			outGenerator.configure(Feature.AUTO_CLOSE_JSON_CONTENT, true);
			
			// Don't display "null" classes
			((ObjectMapper)(outGenerator.getCodec())).getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
			
			outGenerator.writeObject(this);
			outGenerator.close();
		}
		catch (Exception e) {
			e.printStackTrace();
			
			mOutput = null;
		}
		
		return (null == mOutput ? null : mOutput.toString());
	}
}
